#include "config.h"

//#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <vector>
#include "Method.h"
#include "CLI.h"


#include "problem1.h"
#if MODE==1
#include "engine.h"
#include "MatlabTest.h"
#endif

//using namespace RandomLib;
using namespace std;

int main() {
#if CLI_MODE==1
    //CLI::runCLIInterface();
#endif

    #if MODE==1
    Engine *ep;
    if (!(ep = engOpen("\0")))
    {
        fprintf(stderr, "\nCan't start MATLAB engine\n");
    }
    fprintf(stdout, "     *****     Sending data to MATLAB\n");
    #endif

    int problemNumber=1;
    int methodNumber=1;

    Problem *problem=new problem1();
    Method::SubType methodType=(Method::SubType) (methodNumber-1);
    Method *method=Method::createMethod(methodType, problem, 10, 10000);

    #if MODE==1

//MatlabTest::namesVector(ep,problem,method);
//MatlabTest::checkProblemIsSetRight(ep,problem,method);

//MatlabTest::plot3D(ep,problem,method);

       // engEvalString(ep,"save('matlab/workspace00')");
        //engClose(ep);
    #endif



    mxArray *A;
    A=mxCreateDoubleMatrix(problem->getNumberOfProblems(),10,mxREAL);
    double * ptrA=mxGetPr(A);
    ptrA[0]=0.0;
OptimizedValue optStruct;


int i, k, j;
    for(i=0; i<problem->getNumberOfProblems(); i++)
    {
        for(j=0; j<10;j++){
        std::cout<<"___"<<i<<"________";
        optStruct=method->optimize(i);
        std::cout<<"optimized value is : "<<optStruct.value<<" \t \n";
        //should be 10*j+i?
                    ptrA[problem->getNumberOfProblems()*j+i]=optStruct.value;
         }
    }
   engPutVariable(ep,"table",A);
    mxDestroyArray(A);

        engEvalString(ep,"save('matlab/workspace00')");
        engClose(ep);

    #if TEST_VARIABLES==1
       OptimizedValue optStruct=method->optimize(56);
    #endif

    delete problem;
    problem=0;
    delete method;
    method=0;

    return 0;
}


